package org.hljdrl.daf.json;

import org.hljdrl.daf.test.ModuleTest;
import org.hljdrl.daf.xml.Address;
import org.hljdrl.daf.xml.Employee;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;

public class JavaToGJsonTest extends ModuleTest {

	public JavaToGJsonTest(Context ctx) {
		super(ctx);
		setName("Google-Gson");
	}

	private String json;

	@Override
	public String runEncode() {
		StringBuffer mSm = new StringBuffer();
		Employee employee0 = new Employee();
		Address address0 = new Address();
		employee0.setName("Mike");
		employee0.setAge(45);
		employee0.setSalary(20000.45);
		address0.setAddress1("Sagar");
		address0.setAddress2("Nerul");
		address0.setCity("Mumbai");
		address0.setZip(002345);
		employee0.setAddress(address0);
		Gson gson = new Gson();
		String jsonString = gson.toJson(employee0);
		json = jsonString;
		mSm.append(jsonString);
		return mSm.toString();
	}

	@Override
	public String runDecode() {
		StringBuffer mSm = new StringBuffer();
		try {
			Gson gson = new Gson();
			Employee ee = gson.fromJson(json, Employee.class);
			mSm.append(ee.toString());
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return mSm.toString();
	}

}
